package h0913;

import java.util.Arrays;

public class Solution2 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 6, 8, 9, 10, 12};
        System.out.println(getRes2(nums));
    }
    public int getRes(int[] nums){
        if (nums.length<3){
            return 0;
        }
        int current=0;
        int total=0;
        for (int i = 2; i < nums.length ; i++) {
            if (nums[i]-nums[i-1]==nums[i-1]-nums[i-2]){
                current+=1;
                total+=current;
            }else {
                current=0;
            }
        }
        return total;
    }

    public static int getRes2(int[] nums) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            int start = i;
            for (int j = i; j < nums.length; j++) {
                if (j - start + 1 >= 3 && judgeTrue(Arrays.copyOfRange(nums, start, j + 1))) {
                    count++;
                }
            }
        }
        return count;
    }

    private static boolean judgeTrue(int[] nums) {
        int m = nums[1] - nums[0];
        for (int i = 1; i < nums.length - 1; i++) {
            if (nums[i + 1] - nums[i] != m) {
                return false;
            }
        }
        return true;
    }
}
